/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>CreateTime: 2006-4-14 </p>
 * @author pengz
 * @version 1.0
 */

package cn.agiledata.bank.common.tag;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.taglib.TagUtils;

import cn.agiledata.bank.common.servlet.InitForString;

public class AgileChoiceCityTag extends BodyTagSupport {

	public int doStartTag() throws JspException {
		return (EVAL_BODY_TAG);
	}

	public int doEndTag() throws JspException {

		// Render a tag representing the end of our current form
		StringBuffer results = getListData();
		TagUtils.getInstance().write(pageContext, results.toString());
		//System.err.println(results.toString());

		return (EVAL_PAGE);
	}

	public StringBuffer getListData() {
		
		StringBuffer rs = new StringBuffer();

		List id = InitForString.getListId("CITY_INFO");

		List name = InitForString.getListName("CITY_INFO");

		rs.append("\n<script language='JavaScript'>\n");

		rs.append("var cityIdArray=");
		rs.append(creatArray(rs, id));
		rs.append("\n");
		rs.append("var cityNameArray=");
		rs.append(creatArray(rs, name));
		
		rs.append("\n</script>\n");
		return rs;
	}

	private StringBuffer creatArray(StringBuffer ssb, List list) {
		StringBuffer sb = new StringBuffer();
		//System.err.println(list);
		sb.append("new Array(\"");
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i)).append("\",\"");
		}
		sb.append("end\");");
		//System.err.println("sb=="+sb);
		return sb;
		
	}

}
